package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.handler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-29
 */
@Api(description = "教师管理")
@RestController
@RequestMapping("/eduservice/edu_teacher")
//@CrossOrigin//跨域
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping
    public R list(){
/*
        //自定义异常处理，手动抛出异常
        try {
            int a = 10/0;
        }catch(Exception e) {
            throw new GuliException(20001,"出现自定义异常");
        }*/

        List<EduTeacher> list = eduTeacherService.list(null);

        return R.ok().data("list",list);
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){

        boolean remove = eduTeacherService.removeById(id);

        if(remove){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @ApiOperation(value = "根据分页查询讲师信息")
    @GetMapping("getTeacherPage/{page}/{limit}")
    public R getTeacherPage(@PathVariable Long page,
                            @PathVariable Long limit){

        Page<EduTeacher> pageParam = new Page<>(page,limit);

         eduTeacherService.page(pageParam, null);

        List<EduTeacher> records = pageParam.getRecords();

        long total = pageParam.getTotal();
        //方式一
        /*Map<String,Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total",total);
        return R.ok().data(map);*/
        //方式二
        return R.ok().data("records",records).data("total",total);
    }

    @ApiOperation(value = "带条件的分页查询讲师信息")
    @PostMapping("getTeacherPageVo/{page}/{limit}")
    public R getTeacherPageVo(@PathVariable Long page,
                              @PathVariable Long limit,
                              @RequestBody TeacherQuery teacherQuery){

        //取出查询条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断查询条件是否为空
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            queryWrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_create",end);
        }


        Page<EduTeacher> pageParam = new Page<>(page,limit);

        eduTeacherService.page(pageParam, queryWrapper);

        List<EduTeacher> records = pageParam.getRecords();

        long total = pageParam.getTotal();
        //方式一
       /* Map<String,Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total",total);
        return R.ok().data(map);
        //方式二*/
        return R.ok().data("records",records).data("total",total);


    }

    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "根据id查询讲师信息")
    @GetMapping("getTeacherById/{id}")
    public R getTeacherById(@PathVariable String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("eduTeacher",eduTeacher);
    }
    @ApiOperation(value = "修改讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean update = eduTeacherService.updateById(eduTeacher);
        if(update){
            return R.ok();
        }else {
            return R.error();
        }
    }


}










